#!/bin/bash

# params here
peerThreshold=100
# ethver=(1 0)
ethver=(3 2)
rpcpath=$(pwd -P)/data/00/geth.ipc

# 1. run all nodes
# bash run.sh $1 $2

# 2. check network
counter=0
while :
do
    peers=$(./geth attach $rpcpath --exec "net.peerCount")
    if [ $peers -ge $peerThreshold ]; then
        break
    else
        counter=$((counter + 1))
        if [ $counter -gt 3 ]; then
            echo "already wait 30s, but network is still wrong"
            exit 1
        fi
        echo "$peers < $peerThreshold, wait 10s"
        sleep 10
    fi
done
echo "step 2 ok, enough peers!"

# 3. make some block
./geth attach $rpcpath --exec "miner.start(1);admin.sleepBlocks(10);miner.stop();"
echo "step 3 ok, mine some blocks!"

# 4. make txs and mine blocks
process() {
pending=$(./geth attach $rpcpath --exec "txpool.status.pending")
if [ $pending != 10000 ]; then
    echo "something wrong with txpool.pending: $pending"
    exit 1
fi
./geth attach $rpcpath --exec "miner.start(1);admin.sleepBlocks(100);miner.stop();"
pending=$(./geth attach $rpcpath --exec "txpool.status.pending")
if [ $pending != 0 ]; then
    echo "something wrong with txpool.pending: $pending"
    exit 1
fi
}
# transfer-alias
./sendtx $rpcpath ./ ${ethver[0]} 0 > /dev/null
echo "step 4-1 50%, transfer-alias tx ok!"
process
echo "step 4-1 ok, transfer-alias blk ok!"
# sccall-alias
./sendtx $rpcpath ./ ${ethver[0]} 1 > /dev/null
echo "step 4-2 50%, sccall-alias tx ok!"
process
echo "step 4-2 ok, sccall-alias blk ok!"
# transfer-native
./sendtx $rpcpath ./ ${ethver[1]} 0 > /dev/null
echo "step 4-3 50%, transfer-native tx ok!"
process
echo "step 4-3 ok, transfer-native blk ok!"
# sccall-native
./sendtx $rpcpath ./ ${ethver[1]} 1 > /dev/null
echo "step 4-4 50%, sccall-native tx ok!"
process
echo "step 4-4 ok, sccall-native blk ok!"

# 5. blk size statistics
blknum=$(./geth attach $rpcpath --exec "eth.blockNumber")
./sizeblk $rpcpath 10 $blknum > blkstatistics
echo "step 5 ok, blk size statistics!"

# 6. finish
killall geth
echo "end!"